from bs4 import BeautifulSoup, element
from pathlib import Path
import os
import logging
logging.basicConfig(level=logging.DEBUG)

def analyse_item(div):
    sender = div.find("p")
    send_time = div.find("span")
    msg = div.find(class_="content")
    try:
        message = msg.contents[0]
        if type(message) == element.Tag:
            message = message["src"].replace("\n", "")
        return (sender.contents[0], send_time.contents[0], message)
    except Exception as e:
        logging.error("Analyse exception error:", e)

def main(): 
    user_home = os.path.expanduser('~')
    file_name = input('Input the path of file which need deal:')
    obj_h5_file = Path(user_home, file_name)

    logging.debug("begin analyse.")

    if obj_h5_file.exists() == False:
        print(f"{obj_h5_file} not exists.")
        return -1

    soup = BeautifulSoup(open(obj_h5_file, encoding="utf-8"), "lxml")
    logging.debug("has loaded!")
    count = 0
    for div in soup.body.find_all("div", class_='item'):
        if count < 1000 and count > 980:
            r = analyse_item(div)
            print(r)
        count = count + 1
    print("total num:", count)

if __name__ == '__main__':
    main()